﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.BLL;
using Pheonix.Fashionzoomer.DAL;
using System.Text;
using Pheonix.CodeLibrary.Data;

public partial class admin_userdetail : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - User Details [ Admin ]";
        if (!IsPostBack)
        {
            try
            {
                BindControls();

                if (Request["id"] != null)
                {
                    int id = 0;
                    if (int.TryParse(Request["id"], out id))
                        LoadEditData(id);
                }
            }
            catch (Exception ex)
            {
                uccResponseMessage.SetFailMessage("There was a problem in loading the page");
            }
        }
        if (Request["id"] == null)
            lblTitle.Text = "Add a new user";
        else
            lblTitle.Text = "Update user";
    }

    private void BindControls()
    {
        CustomCollection<Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership> membership = 
            Pheonix.Fashionzoomer.Entity.SqlServer2000.Membership.GetAllMembership();
        ddlMembership.DataSource = membership;
        ddlMembership.DataTextField = "PackageName";
        ddlMembership.DataValueField = "Id";
        ddlMembership.DataBind();
    }

    private void LoadEditData(int editDataId)
    {
        Pheonix.Fashionzoomer.Entity.SqlServer2000.User editUser = null;
        if (editDataId != 0)
            editUser = Pheonix.Fashionzoomer.Entity.SqlServer2000.User.GetUserById(editDataId);
        if (editUser != null)
        {
            tbxUserName.Text = editUser.UserName;
            tbxUserPassword.Text = UserManager.DecryptPassword(editUser.UserPassword);
            tbxRetypePassword.Text = UserManager.DecryptPassword(editUser.UserPassword);
            tbxUserEmail.Text = editUser.UserEmail;
            cbxIsActive.Checked = editUser.IsActive;
            cbxIsVerified.Checked = editUser.IsVerified;
            ddlMembership.SelectedValue = editUser.MembershipId.ToString();
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int userId = 0;
            Pheonix.Fashionzoomer.Entity.SqlServer2000.User editUser = null;
            string userOldName = string.Empty;

            if (Request["id"] != null)
            {
                if (int.TryParse(Request["id"], out userId))
                {
                    editUser = Pheonix.Fashionzoomer.Entity.SqlServer2000.User.GetUserById(userId);
                    if (editUser != null)
                        userOldName = editUser.UserName;
                }
            }

            string userName = Utility.FilterInputText(tbxUserName.Text);
            if (userName == userOldName || UserManager.IsUserNameAvailable(userName))
            {
                int userMembershipId = 0;
                if (int.TryParse(ddlMembership.SelectedValue, out userMembershipId))
                {
                    string userPassword = Utility.FilterInputText(tbxUserPassword.Text);
                    string userEmail = Utility.FilterInputText(tbxUserEmail.Text);
                    bool active = cbxIsActive.Checked;
                    bool verified = cbxIsVerified.Checked;

                    if (editUser != null)
                    {
                        editUser.UserName = userName;
                        editUser.UserPassword = UserManager.EncryptPassword(userPassword);
                        editUser.UserEmail = userEmail;
                        editUser.IsActive = active;
                        editUser.IsVerified = verified;
                        editUser.MembershipId = userMembershipId;

                        if (editUser.Save())
                        {
                            uccResponseMessage.SetSuccessMessage("User successfuly updated");
                            LoadEditData(userId);
                        }
                        else
                            uccResponseMessage.SetFailMessage("Could not update user");
                    }
                    else
                    {
                        int newUserId = Pheonix.Fashionzoomer.Entity.SqlServer2000.User.CreateNewUser(userName,
                            userEmail, UserManager.EncryptPassword(userPassword), active, verified, string.Empty, DateTime.Now, userMembershipId);
                          
                        if (newUserId > 0)
                        {
                            uccResponseMessage.SetSuccessMessage("User successfuly created");
                            LoadEditData(newUserId);
                        }
                        else
                            uccResponseMessage.SetFailMessage("Could not create user");
                    }
                }
                else
                    uccResponseMessage.SetFailMessage("Membership of user is invalid");
            }
            else
                uccResponseMessage.SetFailMessage("User name is already taken");
        }
        else
            uccResponseMessage.SetFailMessage("Could not save because of invalid data");
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("userlist.aspx");
    }
}
